/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package canteen.sys.view.individual;

import canteen.sys.SessionFactoryClass;
import canteen.sys.model.entity.Feedback;
import canteen.sys.model.entity.LoginMaster;
import canteen.sys.model.generic.HibernateDAOFactory;
import java.util.Calendar;
import java.util.List;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;

/**
 *
 * @author TNT
 */
public class FeedbackTnT extends javax.swing.JInternalFrame {

    HibernateDAOFactory hibernateDAOFactory = HibernateDAOFactory.getInstance();
    DefaultListModel model;
    LoginMaster lm;

    /**
     * Creates new form FeedbackTnT
     */
    public FeedbackTnT() {
        initComponents();
        lm = SessionFactoryClass.getInstance().getUser();
        model = new DefaultListModel();
        if (lm != null) {
            List<Feedback> list = hibernateDAOFactory.getFeedbackDAO().findAllByUserId(lm.getlId(), true);
            if (list != null && list.size() > 0) {
                String item;
                for (Feedback fb : list) {
                    item = "<html><p><font font-weight: bold; color: #689C29>Title:</font>"
                            + "<font color: #689C29>"
                            + "<h4>" + fb.getTitle() + "</h4></font>"
                            + "<font color: #066ECD;font-weight: bold;>Content: </font>"
                            + "<font color: #066ECD;>" + fb.getContent() + "</font></p>"
                            + "<p><font font-weight: bold; color: #689C29>Answer:"
                            + "</font>"
                            + "<font>" + fb.getAnswer()
                            + "</font></p>"
                            + "<p><font font-weight: bold; color: #689C29>Create Date:"
                            + "</font>"
                            + "<font>" + fb.getCreateDate()
                            + "</font></p>"
                            + "</html>";
                    model.addElement(item);
                }
            }
        } else {
            this.dispose();
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        listFeedback = new javax.swing.JList();
        jScrollPane2 = new javax.swing.JScrollPane();
        txtMessage = new javax.swing.JTextArea();
        jButton1 = new javax.swing.JButton();
        cbDay = new javax.swing.JComboBox();
        txtTitle = new javax.swing.JTextField();

        setClosable(true);
        setTitle("Feedback");

        jScrollPane1.setViewportView(listFeedback);

        txtMessage.setColumns(20);
        txtMessage.setRows(5);
        jScrollPane2.setViewportView(txtMessage);

        jButton1.setText("Send");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        cbDay.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1 Week Ago", "1 Month Ago" }));

        txtTitle.setForeground(new java.awt.Color(0, 102, 102));
        txtTitle.setText("Input Title Here");

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(txtTitle, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 260, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(0, 0, Short.MAX_VALUE))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, jScrollPane1)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 386, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jButton1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 90, Short.MAX_VALUE))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .add(0, 0, Short.MAX_VALUE)
                        .add(cbDay, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 175, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(cbDay, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 53, Short.MAX_VALUE)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 345, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(txtTitle, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                    .add(jButton1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 88, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        if (txtTitle.getText() == null || txtTitle.getText().equals("")) {
            JOptionPane.showMessageDialog(this, "Title must input!", "Warning", JOptionPane.WARNING_MESSAGE);
        } else if (txtTitle.getText() == null || txtTitle.getText().equals("")) {
            JOptionPane.showMessageDialog(this, "Message must input!", "Warning", JOptionPane.WARNING_MESSAGE);
        } else {
            Feedback f = new Feedback();
            f.setContent(txtMessage.getText());
            f.setCreateDate(new java.sql.Date(Calendar.getInstance().getTimeInMillis()));
            f.setLoginMaster(lm);
            f.setStatus(true);
            f.setTitle(txtTitle.getText());
            Feedback fResult = hibernateDAOFactory.getFeedbackDAO().makePersistent(f);
            if (fResult != null) {
                String item = "<html><p><font font-weight: bold; color: #689C29>Title:</font>"
                        + "<font color: #689C29>"
                        + "<h4>" + fResult.getTitle() + "</h4></font></p>"
                        + "<p><font color: #066ECD;font-weight: bold;>Content: </font>"
                        + "<font color: #066ECD;>" + fResult.getContent() + "</font></p>"
                        + "<p><font font-weight: bold; color: #689C29>Answer:"
                        + "</font>"
                        + "<font>" + fResult.getAnswer()
                        + "</font></p>"
                        + "<p><font font-weight: bold; color: #689C29>Create Date:"
                        + "</font>"
                        + "<font>" + fResult.getCreateDate()
                        + "</font></p>"
                        + "</html>";
                model.addElement(item);
                listFeedback.setModel(model);
            }
        }
    }//GEN-LAST:event_jButton1ActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox cbDay;
    private javax.swing.JButton jButton1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JList listFeedback;
    private javax.swing.JTextArea txtMessage;
    private javax.swing.JTextField txtTitle;
    // End of variables declaration//GEN-END:variables
}
